<template>
	<!-- 出差表单 -->
	<view class="travel2">
		<view class="in_storageMain animate__animated animate__flipInY" :class="{ animate__flipOutY: animate__flipOutY }">
			<view class="in_storageMain1">
				<view class="text">出差主题：</view>
				<input type="text" v-model="inputs.name" />
			</view>
			<view class="in_storageMain1">
				<view class="text">出差项目：</view>
				<picker mode="selector" :range="options2" @change="onPickerChange">
					<view class="picker" style="color: #9c9c9c">{{ selectedOption || '点击选择' }}</view>
				</picker>
			</view>
			<view class="in_storageMain1">
				<view class="text">发票：</view>
				<view class="BillSelect">
					<picker mode="selector" :range="optionsBill2" @change="onPickerChangeBill">
						<view class="picker" style="color: #9c9c9c">{{ selectedOptionBill || '点击选择' }}</view>
					</picker>
					<button class="addBill" @click="toAddBill()">新增发票</button>
				</view>
			</view>
			<view class="in_storageMain1">
				<view class="text">税前总花费：</view>
				<input type="text" class="cost" v-model="inputs.cost" />
			</view>
			<view class="in_storageMain1">
				<view class="text">付款方式：</view>
				<picker mode="selector" :range="optionsPay" @change="onPickerChangePay">
					<view class="picker" style="color: #9c9c9c">{{ selectedOptionPay || '点击选择' }}</view>
				</picker>
			</view>
			<view class="in_storageMain1">
				<view class="text">备注：</view>
				<textarea class="beizhu" v-model="inputs.remark"></textarea>
			</view>

			<button class="submit" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
import 'animate.css';
import { request } from '../../request';
export default {
	data() {
		return {
			inputs: {
				name: null,
				cost: null,
				bill: null,
				project: null,
				paymentMethod: null,
				remark: null
			},
			// 所有项目
			options1: ['0', '1', '2'], // 下拉选项
			options2: ['公司', '项目1', '项目2'], // 下拉选项
			selectedOption: '', // 当前选中的选项
			// 发票选择
			optionsBill1: ['1', '2', '3'], // 下拉选项
			optionsBill2: ['发票1', '发票2', '发票3'], // 下拉选项
			selectedOptionBill: '', // 当前选中的选项
			// 付款方式选择
			optionsPay: ['公户', '现金', '公司赊销'], // 下拉选项
			selectedOptionPay: '', // 当前选中的选项,
			// 动画控制开启关闭（页面）
			animate__flipOutY: false
		};
	},
	methods: {
		// 项目点击事件
		onPickerChange(e) {
			const index = e.detail.value; // 获取选中的索引
			this.selectedOption = this.options2[index]; // 更新选中的值
			this.inputs.project = this.options1[index];
		},
		// 发票点击事件
		onPickerChangeBill(e) {
			const index = e.detail.value; // 获取选中的索引
			this.selectedOptionBill = this.optionsBill2[index]; // 更新选中的值
			this.inputs.bill = this.optionsBill1[index];
		},
		onPickerChangePay(e) {
			const index = e.detail.value; // 获取选中的索引
			this.selectedOptionPay = this.optionsPay[index]; // 更新选中的值
			this.inputs.paymentMethod = this.optionsPay[index];
		},
		// 增加发票
		toAddBill() {
			uni.navigateTo({
				url: '/pages/inStorage/addBill'
			});
		},
		submit() {
			request('public/addBusinessTrip', this.inputs, 'POST').then((res) => {
				console.log(res);
			});
			this.animate__flipOutY = true;
			setTimeout(() => {
				uni.navigateBack();
			}, 800);
		}
	},
	onLoad() {
		// 获取所有仓库
		request('public/selectAllProjectAndWarehouse', {}, 'GET').then((res) => {
			const tableItems = res.data.data;
			this.options2 = [];
			this.options1 = [];
			for (var i = 0; i < res.data.data.length; i++) {
				this.options2.push(res.data.data[i].name);
				this.options1.push(res.data.data[i].id);
			}
		});
	},
	onShow() {
		// 获取发票
		request('public/selectAllBill', {}, 'GET').then((res) => {
			// 后续再处理
			if (res.data.code != 200) {
				this.$message.error('   ' + res.data.msg);
				return;
			}
			this.optionsBill1 = [];
			this.optionsBill2 = [];
			for (var i = 0; i < res.data.data.length; i++) {
				this.optionsBill1.push(res.data.data[i].id);
				this.optionsBill2.push(res.data.data[i].name + '  --  ' + res.data.data[i].taxRate + '%');
			}
		});
	}
};
</script>

<style scoped>
.travel2 {
	display: flex;
	justify-content: center;
	padding: 40rpx 20rpx;
	background-color: #f8f9fa;
	min-height: 90vh;
}

.in_storageMain {
	width: 100%;
	max-width: 650rpx;
	background-color: #ffffff;
	border-radius: 20rpx;
	padding: 40rpx 30rpx;
	box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.08);
	height: 1350rpx;
}

.in_storageMain1 {
	width: 100%;
	margin: 30rpx 0;
	display: flex;
	flex-direction: column;
}

.text {
	font-size: 28rpx;
	color: #666666;
	margin-bottom: 15rpx;
	font-weight: 500;
}

input,
.picker {
	width: 92%;
	height: 80rpx;
	border-radius: 12rpx;
	border: 1px solid #e0e0e0;
	padding: 0 25rpx;
	font-size: 28rpx;
	color: #333333;
	transition: all 0.2s ease;
}

input:focus,
.picker:focus {
	border-color: #3498db;
	box-shadow: 0 0 0 2rpx rgba(52, 152, 219, 0.2);
}

.picker {
	display: flex;
	align-items: center;
	background-color: #ffffff;
	position: relative;
}

.picker::after {
	content: '▾';
	position: absolute;
	right: 25rpx;
	color: #999999;
}

.addBill {
	margin-top: 15rpx;
	padding: 15rpx 25rpx;
	background-color: #3498db;
	color: #ffffff;
	border-radius: 10rpx;
	font-size: 26rpx;
	text-align: center;
	transition: all 0.2s ease;
}

.addBill:hover {
	background-color: #2980b9;
}

.BillSelect {
	height: 80rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
}
.BillSelect button {
	margin: 0;
	height: 100%;
	display: flex;
	justify-content: center;
	align-items: center;
}

.BillSelect .picker {
	width: 440rpx !important;
}

.beizhu {
	width: 92%;
	height: 200rpx;
	padding: 20rpx 25rpx;
	border-radius: 12rpx;
	border: 1px solid #e0e0e0;
	font-size: 28rpx;
	color: #333333;
	transition: all 0.2s ease;
	resize: none;
}

.beizhu:focus {
	border-color: #3498db;
	box-shadow: 0 0 0 2rpx rgba(52, 152, 219, 0.2);
}
.submit {
	background-color: #2fb0bc;
	color: white;
	border: 0;
	margin-top: 50rpx;
}
.cost {
	width: ;
}
.cost::after {
	content: '元';
	position: absolute;
	right: 50rpx;
	margin-top: -40rpx;
	transform: translateY(-50%);
	color: #999;
	pointer-events: none;
}
</style>
